import { Component, OnInit } from '@angular/core';
import { User } from '../../_common/_model/user';
import { AppConsts } from '../../_common/consts';
import { Cookie } from 'ng2-cookies';
import { Router } from '@angular/router';
import { Functions } from '../../_common/functions';

@Component({
  selector: 'app-sider-user',
  templateUrl: './user.component.html',
  styleUrls: ['./user.component.less']
})
export class SiderUserComponent implements OnInit {
  isModalVisible: boolean;
  currentUser: User;
  avatar: string;
  constructor(
    private _consts: AppConsts,
    private _funcs: Functions,
    private _router: Router
  ) { }

  ngOnInit(): void {
    this.isModalVisible = false;
    this.currentUser = this._funcs.getCurrentUser();
    if (!this.currentUser.avatar) {
      this.avatar = this.currentUser.nickname.slice(0, 1).toUpperCase();
    }
  }

  handleClickSettings() {
    this.isModalVisible = true;
  }

  handleCancel(e) {
    this.isModalVisible = false;
  }

  logout() {
    Cookie.delete(this._consts.authCookieName);
    location.reload();  // clear cache!!!
    this._router.navigate(['/login']);
  }
}
